package com.sheng.whyclass.person.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.sheng.whyclass.R;
import com.sheng.whyclass.person.fragment.ManagerStudentFragment;
import com.sheng.whyclass.person.fragment.TeacherInfoFragment;
import com.sheng.whyclass.person.view.MainActNavigateSixView;
import com.sheng.whyclass.work.fragment.AddAnswerFragment;
import com.sheng.whyclass.work.fragment.AddWorkFragment;
import com.sheng.whyclass.work.fragment.StatisticsFragment;
import com.sheng.whyclass.work.fragment.UpdateWorkFragment;

public class TeacherMainActivity extends FragmentActivity {

    private TeacherInfoFragment teacherInfoFragment = null;
    private AddWorkFragment addWorkFragment = null;
    private UpdateWorkFragment updateWorkFragment = null;
    private AddAnswerFragment addAnswerFragment = null;
    private ManagerStudentFragment managerStudentFragment = null;
    private StatisticsFragment statisticsFragment = null;

    private NavigateTab curBottomTab = null;

    private MainActNavigateSixView mainActNavigateView;

    public enum NavigateTab {

        NAVIGATE_TEACHER_INFO,
        NAVIGATE_ADD_WORK,
        NAVIGATE_UPDATE_WORK,
        NAVIGATE_ADD_ANSWER,
        NAVIGATE_MANAGER_STUDENT,
        NAVIGATE_STATISTICS

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher);

        teacherInfoFragment = new TeacherInfoFragment();
        addWorkFragment = new AddWorkFragment();
        updateWorkFragment = new UpdateWorkFragment();
        addAnswerFragment = new AddAnswerFragment();
        managerStudentFragment = new ManagerStudentFragment();
        statisticsFragment = new StatisticsFragment();

        curBottomTab = NavigateTab.NAVIGATE_TEACHER_INFO;
        initView();
        initFragment();
    }

    private void initView() {
        mainActNavigateView = (MainActNavigateSixView) findViewById(R.id.mainActNavigateView);
        mainActNavigateView.setCallback(new MainActNavigateSixView.ICallback() {

            @Override
            public void onClicked(byte component) {
                switch (component) {
                    case MainActNavigateSixView.Component.NAVIGATE_TEACHER_INFO: {
                        switchToNavTab(NavigateTab.NAVIGATE_TEACHER_INFO);
                        break;
                    }
                    case MainActNavigateSixView.Component.NAVIGATE_ADD_WORK: {
                        switchToNavTab(NavigateTab.NAVIGATE_ADD_WORK);
                        break;
                    }
                    case MainActNavigateSixView.Component.NAVIGATE_UPDATE_WORK: {
                        switchToNavTab(NavigateTab.NAVIGATE_UPDATE_WORK);
                        break;
                    }
                    case MainActNavigateSixView.Component.NAVIGATE_ADD_ANSWER: {
                        switchToNavTab(NavigateTab.NAVIGATE_ADD_ANSWER);
                        break;
                    }
                    case MainActNavigateSixView.Component.NAVIGATE_MANAGER_STUDENT: {
                        switchToNavTab(NavigateTab.NAVIGATE_MANAGER_STUDENT);
                        break;
                    }
                    case MainActNavigateSixView.Component.NAVIGATE_STATISTICS: {
                        switchToNavTab(NavigateTab.NAVIGATE_STATISTICS);
                        break;
                    }

                }
            }
        });
    }

    private void initFragment() {
        switchToNavTab(curBottomTab);
    }

    public void switchToNavTab(NavigateTab tab) {
        if (tab == null) {
            return;
        }
        curBottomTab = tab;
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager != null) {
            if (tab == NavigateTab.NAVIGATE_TEACHER_INFO) {

                String wifiTag = TeacherInfoFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            teacherInfoFragment, wifiTag);
                }

                ft.show(teacherInfoFragment);
                ft.hide(addWorkFragment);
                ft.hide(updateWorkFragment);
                ft.hide(addAnswerFragment);
                ft.hide(managerStudentFragment);
                ft.hide(statisticsFragment);

                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateSixView.Component.NAVIGATE_TEACHER_INFO);
            } else if (tab == NavigateTab.NAVIGATE_ADD_WORK) {

                String wifiTag = AddWorkFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            addWorkFragment, wifiTag);
                }

                ft.hide(teacherInfoFragment);
                ft.show(addWorkFragment);
                ft.hide(updateWorkFragment);
                ft.hide(addAnswerFragment);
                ft.hide(managerStudentFragment);
                ft.hide(statisticsFragment);

                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateSixView.Component.NAVIGATE_ADD_WORK);
            } else if (tab == NavigateTab.NAVIGATE_UPDATE_WORK) {

                String wifiTag = UpdateWorkFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            updateWorkFragment, wifiTag);
                }

                ft.hide(teacherInfoFragment);
                ft.hide(addWorkFragment);
                ft.show(updateWorkFragment);
                ft.hide(addAnswerFragment);
                ft.hide(managerStudentFragment);
                ft.hide(statisticsFragment);

                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateSixView.Component.NAVIGATE_UPDATE_WORK);
            } else if (tab == NavigateTab.NAVIGATE_ADD_ANSWER) {

                String wifiTag = AddAnswerFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            addAnswerFragment, wifiTag);
                }

                ft.hide(teacherInfoFragment);
                ft.hide(addWorkFragment);
                ft.hide(updateWorkFragment);
                ft.show(addAnswerFragment);
                ft.hide(managerStudentFragment);
                ft.hide(statisticsFragment);

                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateSixView.Component.NAVIGATE_ADD_ANSWER);
            } else if (tab == NavigateTab.NAVIGATE_MANAGER_STUDENT) {

                String wifiTag = ManagerStudentFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            managerStudentFragment, wifiTag);
                }

                ft.hide(teacherInfoFragment);
                ft.hide(addWorkFragment);
                ft.hide(updateWorkFragment);
                ft.hide(addAnswerFragment);
                ft.show(managerStudentFragment);
                ft.hide(statisticsFragment);

                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateSixView.Component.NAVIGATE_MANAGER_STUDENT);
            } else if (tab == NavigateTab.NAVIGATE_STATISTICS) {

                String wifiTag = StatisticsFragment.class.getSimpleName();

                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (fragmentManager.findFragmentByTag(wifiTag) == null) {
                    ft.add(R.id.main_activity_fragment_container,
                            statisticsFragment, wifiTag);
                }

                ft.hide(teacherInfoFragment);
                ft.hide(addWorkFragment);
                ft.hide(updateWorkFragment);
                ft.hide(addAnswerFragment);
                ft.hide(managerStudentFragment);
                ft.show(statisticsFragment);

                ft.commitAllowingStateLoss();
                mainActNavigateView.setSelected(MainActNavigateSixView.Component.NAVIGATE_STATISTICS);
            }
        }
    }
}
